'''
Created on 31/08/2012

@author: Nrai
'''
import sys
import os
from src.ex.pex import PaletteValidationError

class PaletteFactory(object):
    
    def __init__(self, filename):
        
        pass
            
    def generate_palette(self):
        
        pass
    
    def validate_palette(self, filename):
        '''
        Receives either a palette object and validates it
        '''
        #placeholder, configuration pending
        pproperties = {"act" : {"length" : 768, "mode" : "RGB"}}
        
        try:
            with open(filename) as pfile:
                #get the palette extension
                ptype = pfile.name.split(".")[1]
                #if the palette type is known
                if ptype in pproperties.keys():
                    #get the palette size from the file
                    psize = os.path.getsize(filename)
                    try:
                        #Assert it matches the configuration size
                        assert psize == pproperties[ptype]["length"]
                    except AssertionError:
                        #log pending
                        raise PaletteValidationError(
                                             filename, 
                                             "Size Error, file should be %d \
                                             but found %d instead." 
                                             % (pproperties[ptype]["length"], psize)
                                             )
                    #Read the palette file into a bytearray
                    pdata = bytearray(psize)
                    pfile.readinto(pdata)
                    #Assert the type and range of the palette
                    for index, color in enumerate(pdata):
                        try:
                            assert type(color) is int
                            assert 0 <= color <= 255
                        except AssertionError:
                            #log pending
                            raise PaletteValidationError(
                                                         filename, 
                                                         "Incorrect value or range \
                                                         %d at index %d" % (index, color)
                                                         )
                    #The palette seems to be valid, create the object
                    pmode = pproperties[ptype]["mode"]
                    pchannels = len(pmode)
                    return filename, pdata, ptype, psize, pchannels, pmode
        
        except IOError:
            #log pending
            raise IOError, "File not found"

class Palette(object):
    
    '''
        Base class for handling palettes
    '''
    
    def __init__(self, filename):
        
        self.filename = filename
        self.palette = None
        self.type = self.filename.split(".")[0]
        self.size = os.path.getsize(self.filename)
        self.channels = None
        self.mode = None
        
        
        
    def get_palette(self):
        
        if not self.palette:
            pass
        return self.palette
        
    
        
    def parse_palette(self):
        
        pass   
            
        
        